/*
 * main.c
 *
 *  Created on: 12.05.2012
 *      Author: Eugen Schneider
 */

#include <util/delay.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/pgmspace.h>

#include "adc.h"
#include "timer.h"
#include "sms_struct.h"
#include "sms.h"

#define ADC_CHANNEL 4
#define TEL_NR_BARTETZKO "01707304271"
#define TEL_NR_HAGEMANN "01772661615"

volatile uint16_t adc_val = 0;
volatile uint32_t adc_val_sum = 0;

volatile uint8_t semaphore = 0;

volatile uint8_t flag15min = 0;
volatile uint8_t flag_second = 0;

volatile uint8_t seconds = 0;
uint8_t minutes = 0;
uint8_t hours = 0;
uint8_t day = 0;
uint8_t month = 0;
uint16_t year = 0;

struct sms_payload sms;

void int2binary(unsigned int n, char *buffer, unsigned int buffer_size) {
	unsigned int i = (buffer_size - 1);
	buffer[i] = '\0';

	while (i > 0) {
		if (n & 0x01) {
			buffer[--i] = '1';
		} else {
			buffer[--i] = '0';
		}

		n >>= 1;
	}
}

int main() {

	uint8_t val_counter = 0;

	bInitTimer();
	bInitADC();
	cli();

	sms_init();

	////////////////////////////////////////////	Test SMS

		sms_time(&year, &month, &day, &hours, &minutes, &seconds);

		char string[165] = "";
		char tmpstr[20] = "";

		sprintf(tmpstr, "%02X", seconds);
		strcat(string, tmpstr);

		sprintf(tmpstr, "%02X", minutes);
		strcat(string, tmpstr);

		sprintf(tmpstr, "%02X", hours);
		strcat(string, tmpstr);

		sprintf(tmpstr, "%02X", day);
		strcat(string, tmpstr);

		sprintf(tmpstr, "%02X", month);
		strcat(string, tmpstr);

		sprintf(tmpstr, "%02X", year);
		strcat(string, tmpstr);

		for (uint8_t tmpvar = 0; tmpvar < MAX_SMS_VALUES; ++tmpvar) {
			if (tmpvar % 2) {

				sms.value[tmpvar] = 0x0505;
			} else
				sms.value[tmpvar] = 0xFFFF;

			sprintf(tmpstr, "%04X", sms.value[tmpvar]);
			strcat(string, tmpstr);

		}
		sms_send(TEL_NR_BARTETZKO, string);

	/////////////////////////////////////////////////////////////////////////////////////

	//	sms_display_text(string);


	sei();

	while (1) {

		if (flag_second) {

			flag_second = 0;
			if (seconds >= 60) {
				seconds = 0;
				minutes++;
				if (!(minutes % 15)) {

					flag15min = 1;
				}
				if (minutes >= 60) {

					minutes = 0;
					hours++;
					if (hours >= 24) {
						hours = 0;
						day++;

						if (day >= 29 && month == 2 && (year % 4)) {

							day = 1;
							month++;
							if (month >= 13) {

								month = 1;
								year++;
							}
						} else if (day >= 30 && month == 2 && !(year % 4)) {

							day = 1;
							month++;
							if (month >= 13) {

								month = 1;
								year++;
							}

						} else if (day >= 31 && (month == 4 || month == 6
								|| month == 9 || month == 11)) {

							day = 1;
							month++;
							if (month >= 13) {

								month = 1;
								year++;
							}

						} else if (day >= 32 && (month == 1 || month == 3
								|| month == 5 || month == 7 || month == 8
								|| month == 10 || month == 12)) {

							day = 1;
							month++;
							if (month >= 13) {

								month = 1;
								year++;
							}

						}

						// @TODO update date maybe?
					}
				}

			}

			/////////////////////////////////////////test printf
//			printf("Time since start: %02d:%02d:%02d \n", hours, minutes,
//					seconds);
//
//			printf(
//					"Current ADC value: %06u\nCurrent sum of ADC values: %06lu \n",
//					adc_val, adc_val_sum);
//
//			char buffer[64];
//
//			int2binary(adc_val_sum, buffer, sizeof buffer);
//
//			printf("Current sum of ADC values (binary): %s\n", buffer);
//
//			for (int var = 0; var < MAX_SMS_VALUES; ++var) {
//				uint32_t tmpsms;
//				tmpsms = sms.value[var] << 5;
//				printf("Value %02u : %06lu\n", var, tmpsms);
//
//			}
			/////////////////////////////////////////////////////
		}

		if (flag15min) {
			while (semaphore) {

				;
			}
			adc_val_sum = adc_val_sum >> 4;
			sms.value[val_counter] = (uint16_t) (adc_val_sum);

			flag15min = 0;
			adc_val_sum = 0;
			val_counter++;
			if (val_counter >= MAX_SMS_VALUES) {

				sms.time_and_date.seconds = seconds;
				sms.time_and_date.minutes = minutes;
				sms.time_and_date.hours = hours;
				sms.time_and_date.day = day;
				sms.time_and_date.month = month;
				sms.time_and_date.year = year;

				char string[165] = "";
				char tmpstr[20] = "";
				sprintf(tmpstr, "%02X", sms.time_and_date.seconds);
				strcat(string, tmpstr);

				sprintf(tmpstr, "%02X", sms.time_and_date.minutes);
				strcat(string, tmpstr);

				sprintf(tmpstr, "%02X", sms.time_and_date.hours);
				strcat(string, tmpstr);

				sprintf(tmpstr, "%02X", sms.time_and_date.day);
				strcat(string, tmpstr);

				sprintf(tmpstr, "%02X", sms.time_and_date.month);
				strcat(string, tmpstr);

				sprintf(tmpstr, "%02X", sms.time_and_date.year);
				strcat(string, tmpstr);

				//				uint8_t tmpvar;

				for (int var = 0; var < MAX_SMS_VALUES; ++var) {

					sprintf(tmpstr, "%04X", sms.value[val_counter]);
					strcat(string, tmpstr);
					sms.value[val_counter] = 0;

				}
				sms_send(SMS_TELNR, string);

				val_counter = 0;

			}

		}

	}
}

ISR(TIMER1_COMPA_vect)
{
	seconds++;
	semaphore = 1;
	adc_val = ui16ReadADC(ADC_CHANNEL);
	adc_val_sum += adc_val;
	semaphore = 0;
	flag_second = 1;
}
